package lwx;

import java.util.HashMap;


public class Outer{
    private void test(final int i){
        new Service(){
            public void method(){
                for (int j = 0; j < i; j++) {
                    System.out.println("匿名内部类");
                }
            }
        }.method();
    }
    interface Service{
        void method();
    }

    public static int lengthOfLastWord(String s){
        int res=0;
        int end=s.length()-1;
        while (end>=0&&s.charAt(end)==' '){
            end--;
        }
        if (end<0){
            return res;
        }
        int start=end;
        while(start>=0&&s.charAt(start)!=' '){
            start--;
        }
        res=end-start;
        return res;
    }
    public static void main(String[] args) {
        /*int s=lengthOfLastWord("a");
        HashMap<Integer,String> hashMap = new HashMap<>();
        hashMap.put(1,"s");
        ConcurrentHashMap<Integer,String> concurrentHashMap=new ConcurrentHashMap<>();
        concurrentHashMap.put(1,"s");*/
        int[] nums ={1,1,1,2,2,3};
        System.out.println(removeDuplicates(nums,2));
    }



    public static int removeDuplicates(int[] nums,int k){
        int u=0;
        for(int x:nums){
            if(u<k||nums[u-k]!=x){
                nums[u++]=x;
            }
        }
        return u;
    }


    public static boolean isIsomorphic(String s, String t){
        if (s.length()!=t.length()){
            return false;
        }
        HashMap<Character,Character> s2t=new HashMap<>(),t2s=new HashMap<>();
        for (int i=0;i<s.length();i++){
            if (s2t.containsKey(s.charAt(i))||t2s.containsKey(t.charAt(i))){
                if(t.charAt(i)!=s2t.get(s.charAt(i))||s.charAt(i)!=t2s.get(t.charAt(i))){
                    return false;
                }
            }else{
                s2t.put(s.charAt(i),t.charAt(i));
                t2s.put(t.charAt(i),s.charAt(i));
            }
        }
        return true;
    }

    /**
     * pattern = "abba", s = "dog cat cat dog"
     * 输出: true
     * @param pattern
     * @param s
     * @return
     */
    public static boolean wordPattern(String pattern, String s){
        HashMap<Character,String> hashMap=new HashMap<>();
        String[] split=s.split(" ");
        if (pattern.length()!= split.length){
            return false;
        }
        for (int i = 0; i < pattern.length(); i++) {
            if (hashMap.containsKey(pattern.charAt(i))){
                if(!hashMap.get(pattern.charAt(i)).equals(split[i])){
                    return false;
                }
            }else{
                hashMap.put(pattern.charAt(i),split[i]);
            }
        }
        return true;
    }

    public static boolean canConstructHash(String ransomNote, String magazine){
        if (ransomNote.length()>magazine.length()){
            return false;
        }
        HashMap<Character,Integer> hashMap=new HashMap<>();
        for(char c:ransomNote.toCharArray()){
            if (hashMap.containsKey(c)){
                hashMap.put(c,hashMap.get(c)+1);
            }
            hashMap.put(c,1);
        }
        for(char c:magazine.toCharArray()){
            if (hashMap.containsKey(c)){
                hashMap.put(c,hashMap.get(c)-1);
                if (!hashMap.containsKey(c)||hashMap.get(c)<0){
                    return false;
                }
            }

        }
        return true;
    }
    public static boolean canConstruct(String ransomNote, String magazine) {
        if(ransomNote.length()>magazine.length()){
            return false;
        }
        int[] cnt=new int[26];
        for(char c:magazine.toCharArray()){
            cnt[c-'a']++;
        }
        for(char c:ransomNote.toCharArray()){
            int res=c-'a';
            cnt[c-'a']--;
            if(cnt[c-'a']<0){
                return false;
            }
        }
        return true;
    }

    /**
     * 验证是否为回文字符串
     * @param s
     * @return
     */
    public static boolean isPalindrome(String s) {
        Boolean res=Boolean.TRUE;
        int len=s.length();
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < len; i++) {
            if (Character.isLetterOrDigit(s.charAt(i))){
                sb.append(Character.toLowerCase(s.charAt(i)));
            }
        }
        int p=0,q=len-1;
        while(p!=q){
            if(sb.charAt(p)!=sb.charAt(q)){
                res=Boolean.FALSE;
            }
            p++;
            q--;
        }
        return res;
    }

    public static boolean isPalindromeFast(String s, String t){
        int p=0,q=0;
        boolean res=true;
        while(p<s.length()&&q<t.length()){
            if(s.charAt(p)==t.charAt(q)){
                p++;
            }
            q++;
            if(q==t.length()){
                res=false;
            }
        }
        return res;
    }







    //判断一个数字是否是回文数字，负数按照绝对值判断 （示例：1221是回文数字，-343是回文数字，1234不是回文数字）
    public static boolean isHuiwen(int num){
        if (num%10==0&&num != 0){
            return false;
        }
        if(num <0){
            num = Math.abs(num);
        }
        int revert =0;
        while(num>revert){
            revert = revert *10+num%10;
            num = num/10;
        }
        if (num==revert || num==revert/10){
            return true;
        }else{
            return false;
        }

    }

}